﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnrealDebugger.Panels;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using System.Windows.Forms;

namespace UnrealDebugger
{
    // Document Management

    partial class UnrealDebuggerIDE
    {
        public void ShowDocument(Document document)
        {
            if (document != null)
            {
                document.Show(this.dockPanelDebugger);
            }
        }

        public Document GetDocumentWithFocus()
        {
            return this.dockPanelDebugger.ActiveDocument as Document;
        }

        public List<Document> GetOpenDocuments()
        {
            List<Document> documents = new List<Document>();

            foreach (DockContent content in this.dockPanelDebugger.Contents)
            {
                if (content as Document != null)
                {
                    documents.Add(content as Document);
                }
            }

            return documents;
        }

        public Document GetFirstDocument()
        {
            if (this.dockPanelDebugger.ActiveDocumentPane == null) { return null; }
            if (this.dockPanelDebugger.ActiveDocumentPane.Contents.Count == 0) { return null; }
            return (Document) this.dockPanelDebugger.ActiveDocumentPane.Contents[0];
        }

        public Document GetLastDocument()
        {
            if (this.dockPanelDebugger.ActiveDocumentPane == null) { return null; }
            if (this.dockPanelDebugger.ActiveDocumentPane.Contents.Count == 0) { return null; }
            return (Document)this.dockPanelDebugger.ActiveDocumentPane.Contents[this.dockPanelDebugger.ActiveDocumentPane.Contents.Count - 1];
        }

        public Document GetPreviousDocument()
        {
            bool getPreviousDocument = false;

            if (this.dockPanelDebugger.ActiveDocumentPane != null)
            {
                for (int x = this.dockPanelDebugger.ActiveDocumentPane.Contents.Count - 1; x > -1; x--)
                {
                    DockContent dockContent = (DockContent)this.dockPanelDebugger.ActiveDocumentPane.Contents[x];

                    if (dockContent == this.dockPanelDebugger.ActiveDocumentPane.ActiveContent) { getPreviousDocument = true; }
                    else if (getPreviousDocument) { return (Document)dockContent; }
                }

                if (this.dockPanelDebugger.ActiveDocumentPane.Contents.Count > 0)
                {
                    return (Document)this.dockPanelDebugger.ActiveDocumentPane.Contents[this.dockPanelDebugger.ActiveDocumentPane.Contents.Count - 1];
                }
            }

            return null;
        }

        public Document GetNextDocument()
        {
            bool getNextDocument = false;

            if (this.dockPanelDebugger.ActiveDocumentPane != null)
            {
                for (int x = 0; x < this.dockPanelDebugger.ActiveDocumentPane.Contents.Count; x++)
                {
                    DockContent dockContent = (DockContent)this.dockPanelDebugger.ActiveDocumentPane.Contents[x];

                    if (dockContent == this.dockPanelDebugger.ActiveDocumentPane.ActiveContent) { getNextDocument = true; }
                    else if (getNextDocument) { return (Document)dockContent; }
                }

                if (this.dockPanelDebugger.ActiveDocumentPane.Contents.Count > 0)
                {
                    return (Document)this.dockPanelDebugger.ActiveDocumentPane.Contents[0];
                }
            }

            return null;
        }

        public Document GetDocumentFromClassname(string className)
        {
            string[] tokens = className.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            string packageName = tokens.Length > 0 ? tokens[0] : "Unreal";
            string scriptName = tokens.Length > 1 ? tokens[1] : "Class";
            string fullpath = string.Format(@"{0}\{1}\Classes\{2}.uc", Project.ProjectPath.TrimEnd('\\'), packageName, scriptName);

            return GetDocumentFromFile(fullpath);
        }

        public Document GetDocumentFromFile(string filename)
        {
            Document document = null;
            string fullpath = Path.GetFullPath(filename).TrimEnd('\\');

            this._explorerPanel.AddToRecentFiles(filename);

            foreach (DockContent content in this.dockPanelDebugger.Contents)
            {
                if (content as Document != null)
                {
                    bool sameFile = String.Compare(
                        Path.GetFullPath((content as Document).FileName).TrimEnd('\\'),
                        fullpath,
                        StringComparison.InvariantCultureIgnoreCase) == 0;

                    if (sameFile)
                    {
                        document = content as Document;
                        break;
                    }
                }
            }

            if (document == null && File.Exists(fullpath))
            {
                document = new Document();
                document.FileName = fullpath;
            }

            return document;
        }

        public List<FileInfo> SearchFiles(string text)
        {
            return this._explorerPanel.SearchFiles(text);
        }

        private void BuildRecentFilesList()
        {
            this.recentFilesToolStripMenuItem.DropDownItems.Clear();

            foreach (string file in this._explorerPanel.RecentFiles)
            {
                ToolStripMenuItem item = new ToolStripMenuItem(file);
                item.Text = file;
                item.Click += new EventHandler(recentFile_Click);
                this.recentFilesToolStripMenuItem.DropDownItems.Add(item);
            }
        }

        private void OnDocumentShown(Document document)
        {
            BuildRecentFilesList();
        }

        void recentFile_Click(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            if (item != null) { this._explorerPanel.ShowDocument(item.Text); }
        }
    }
}
